package hanxx.security.dao;

import hanxx.security.domain.SysDept;
import hanxx.system.dao.BaseDao;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.io.Serializable;
import java.util.List;

/**
 * 部门管理 数据层
 */
public interface SysDeptDao extends BaseDao<SysDept, Serializable> {

    /**
     * 获取机构跟部门
     *
     * @param orgId 机构id
     * @param parentId,父部门id，0
     * @return 部门信息
     */
    public SysDept findFirstByOrgIdAndParentId(Long orgId,Long parentId);
    /**
     * 获取机构所有部门
     *
     * @param orgId 机构id
     * @return 部门信息
     */
    public List<SysDept> findAllByOrgIdAndDelFlag(Long orgId,String delFlag);
    /**
     * 获取下级部门
     *
     * @param parentId 机构id
     * @return 部门信息
     */
    public List<SysDept> findAllByParentIdAndDelFlag(Long parentId,String delFlag);
    /**
     * 根据部门ID查询信息
     *
     * @param deptId 部门ID
     * @return 部门信息
     */
    @Query(value = " select * from sys_dept where dept_id =?1 ", nativeQuery = true)
    public SysDept selectDeptById(Long deptId);


    /**
     * 根据ID查询所有子部门
     *
     * @param deptId 部门ID
     * @return 部门列表
     */
    @Query(value = "select * from sys_dept where find_in_set(?1, ancestors)", nativeQuery = true)
    public List<SysDept> selectChildrenDeptById(Long deptId);

    /**
     * 根据ID查询所有子部门（正常状态）
     *
     * @param deptId 部门ID
     * @return 子部门数
     */
    @Query(value = "select count(*) from sys_dept where status = 0 and del_flag = '0' and find_in_set(?1, ancestors)", nativeQuery = true)
    public int selectNormalChildrenDeptById(Long deptId);

    /**
     * 是否存在子节点
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Query(value = "select count(1) from sys_dept where del_flag = '0' and parent_id = ?1 ", nativeQuery = true)
    public int hasChildByDeptId(Long deptId);

    /**
     * 查询部门是否存在用户
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Query(nativeQuery = true, value = "select count(1) from sys_user where dept_id = ?1 and del_flag = '0'")
    public int checkDeptExistUser(Long deptId);

    /**
     * 校验部门名称是否唯一
     *
     * @param deptName 部门名称
     * @param parentId 父部门ID
     * @return 结果
     */
    @Query(nativeQuery = true, value = "select * from sys_dept where dept_name=?1 and parent_id = ?2 limit 1")
    public SysDept checkDeptNameUnique(String deptName, Long parentId);


    /**
     * 删除部门管理信息
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Modifying
    @Query(value = "update sys_dept set del_flag = '2' where dept_id = ?1 ", nativeQuery = true)
    public int deleteDeptById(Long deptId);


    @Modifying
    @Query(value = "update sys_dept set status = :#{#dept.status} ,update_by= :#{#dept.updateBy},update_time=:#{#dept.updateTime} where find_in_set(:#{#dept.deptId}, ancestors)", nativeQuery = true)
    public void updateDeptStatus(@Param("dept") SysDept dept);


    @Modifying
    @Query(value = " update SysDept set leaderDept=false where orgId=?1 ")
    public void resetLeaderDept(Long orgId);

    @Query(" from SysDept where orgId=?1 and leaderDept=true  ")
    public SysDept leaderDept(Long orgId);

    @Query(" from SysDept where parentId>0 and orgId=?1 and leaderDept=false  ")
    public List<SysDept> commonDept(Long orgId);


}
